package com.simplefan.system.client.web.rest;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.simplefan.framework.common.rest.RestObjectAnalyzerFactory;
import com.simplefan.meta.MetaObject;
import com.simplefan.meta.rest.MetaObjectRestObjectAnalyzer;
import com.simplefan.system.common.Rest.DoubleRestObjectAnalyzer;
import com.simplefan.system.common.Rest.FloatRestObjectAnalyzer;
import com.simplefan.system.common.Rest.IntegerRestObjectAnalyzer;
import com.simplefan.system.common.Rest.ListRestObjectAnalyzer;
import com.simplefan.system.common.Rest.LongRestObjectAnalyzer;
import com.simplefan.system.common.Rest.StringRestObjectAnalyzer;

public class RestFaceController extends HttpServlet {

  
  @Override
  public void init() throws ServletException {

    RestObjectAnalyzerFactory.registerRestObjectAnalyzer("string", String.class.getName(), new StringRestObjectAnalyzer());
    RestObjectAnalyzerFactory.registerRestObjectAnalyzer("double", Double.TYPE.getName(), new DoubleRestObjectAnalyzer());
    RestObjectAnalyzerFactory.registerRestObjectAnalyzer("float", Float.TYPE.getName(), new FloatRestObjectAnalyzer());
    RestObjectAnalyzerFactory.registerRestObjectAnalyzer("int", Integer.TYPE.getName(), new IntegerRestObjectAnalyzer());
    RestObjectAnalyzerFactory.registerRestObjectAnalyzer("long", Long.TYPE.getName(), new LongRestObjectAnalyzer());
    
//    RestObjectAnalyzerFactory.registerRestObjectAnalyzer("String", String.class.getName(), new StringRestObjectAnalyzer());
    RestObjectAnalyzerFactory.registerRestObjectAnalyzer("Double", Double.class.getName(), new DoubleRestObjectAnalyzer());
    RestObjectAnalyzerFactory.registerRestObjectAnalyzer("Float", Float.class.getName(), new FloatRestObjectAnalyzer());
    RestObjectAnalyzerFactory.registerRestObjectAnalyzer("Integer", Integer.class.getName(), new IntegerRestObjectAnalyzer());
    RestObjectAnalyzerFactory.registerRestObjectAnalyzer("Long", Long.class.getName(), new LongRestObjectAnalyzer());

    RestObjectAnalyzerFactory.registerRestObjectAnalyzer("metaobject", MetaObject.class.getName(), new MetaObjectRestObjectAnalyzer());
    RestObjectAnalyzerFactory.registerRestListAnalyzer(new ListRestObjectAnalyzer());
    
    
    super.init();
  }


  @Override
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String[] infos = request.getPathInfo().split("/");

    String result = null;
    if (infos == null || infos.length != 3) {
      result = facadeConnector.connectMethod(null, null, null);
    }
    else {
      String moduleName = infos[1];
      String methodName = infos[2];
      String parameterXml = request.getParameter("parameter");

      result = facadeConnector.connectMethod(moduleName, methodName, parameterXml);
    }
    
    response.getWriter().write(result);
  }
  
  
  private FacadeConnector facadeConnector = new FacadeConnector();
}
